package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.CarriageInfo;
import com.sgb.kjwl.model.entity.transport.CarsSourcesEntiity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.StringUtil;

import java.util.List;

public class SourceOfCarAdapter extends BaseRecycleAdapter<CarriageInfo> {

    private Context context;
    private boolean isHide = false;

    public SourceOfCarAdapter(Context context, List<CarriageInfo> datas) {
        super(context, R.layout.item_car_list, datas);
        this.context = context;
    }

    public SourceOfCarAdapter(Context context, List<CarriageInfo> datas, boolean isHide) {
        super(context, R.layout.item_car_list, datas);
        this.context = context;
        this.isHide = isHide;
    }

    @Override
    public void convert(ItemViewHolder holder, final CarriageInfo entity) {
        holder.setBinding(BR.viewModel, entity);

        TextView textView = holder.getItem().findViewById(R.id.tv_distance);
        TextView tvTag=holder.getItem().findViewById(R.id.tv_tag);
        TextView tvTime=holder.getItem().findViewById(R.id.company_view);
        TextView tvPrice=holder.getItem().findViewById(R.id.price_view);

        if (entity.getFreight() != null) {
            String priceText = entity.getFreight();
            if (priceText.indexOf(".") > 0) {
                priceText = priceText.replaceAll("0+?$", "");//去掉多余的0
                priceText = priceText.replaceAll("[.]$", "");//如最后一位是.则去掉
            }
            tvPrice.setText(priceText + entity.getUnit());
        }else {
            tvPrice.setText("面议");
        }

        tvTime.setText("装货时间:"+ DateUtils.timestampTo13DateNos(entity.getShipmentStartTime())+"-"+DateUtils.timestampTo13DateNos(entity.getShipmentEndTime()));


        if (entity != null) {
            if (!isHide) {
                if (TextUtils.isEmpty(entity.getDistance()) || TextUtils.equals(entity.getDistance(), "null")) {
                    textView.setText("");
                } else {
                    textView.setText(Double.valueOf(entity.getDistance()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getDistance()) + "KM");
                }
            }
            tvTag.setVisibility(View.GONE);
        }
    }

}
